#include "../../includes/controller/ThreadConsole.h"
#include "../../includes/controller/ServidorAplicacao.h"

namespace controller{

	ThreadConsole::ThreadConsole( ServidorAplicacao *servidor, FILE *in, QObject *parent ) :
		QThread( parent ), servidor( servidor )
	{
		input = new QTextStream( in, QIODevice::ReadOnly );
	}

	ThreadConsole::~ThreadConsole()
	{
		delete input;
	}

	void ThreadConsole::run()
	{
		QString line;
		bool exit = false;

		Logger::write( "TheadConsole iniciada..." );

		while( ! exit )
		{
			msleep( 500 );

			*input >> line;

			exit = ( line == "exit" || line == "quit" );

			if( line ==  "-t" || line == "--t" )
			{
				Logger::write( servidor->getThreadsAtivas() );
			} else if( line == "-T" || line == "--T" )
			{
				Logger::write( servidor->getTodasThreadsAtivas() );
			} else if( line == "-a" || line == "--a" )
			{
				servidor->listaArquivosAbertosClientes();
			}
		}

		emit terminated();
	}
}
